package com.example.qianfeng.image.stream;

import java.io.File;
import java.time.LocalDate;
import java.util.*;

public class LimitInputStream {
    private final static File IMG_PATH = new File("D:\\pictures\\statics\\img\\user002");
    private final static String IMG_PATH_TEMP_STRING = "G:\\pictures\\statics\\img\\user002\\temp";
    private final static String IMG_PATH_DIR_STRING = "G:\\pictures\\statics\\img\\user002";

    public static void main(String[] args) {
        // 递归删除临时文件
        File file = new File(IMG_PATH_TEMP_STRING);
        deleteDirectoryLegacyIO(file);

        // 删除过期的文件夹 1. 找出所有的文件夹 2. 把过期的文件夹及文件都删除
        File dir = new File(IMG_PATH_DIR_STRING);
        List<String> dirNameList;
        dirNameList = findAllDirs(dir);
        Map<String, Long> map = new HashMap<>();
        for (String name : dirNameList) {
            map.put(name, ImageFormatChecker.getTimeStampSys(name));
        }
        if (dirNameList != null && dirNameList.size() > 0) {
            executeOutTimeFile(map, dirNameList);
        }
    }

    /**
     * 过期文件删除
     *
     * @param map 文件夹名称对应的时间戳
     * @param dirNameList 文件夹名称列表
     */
    private static void executeOutTimeFile(Map<String, Long> map, List<String> dirNameList) {
        for (String name : dirNameList) {
            Long timestamp = map.get(name);
            Date date = new Date(timestamp); // 上传日期
            Calendar calendar = Calendar.getInstance();
            Date nowTime = calendar.getTime(); // 当前日期
            calendar.setTime(date);
            calendar = initCalender(calendar);
            Date outTime = calendar.getTime(); // 过期时间
            if (nowTime.after(outTime)) {
                deleteDirectoryLegacyIO(new File(IMG_PATH_DIR_STRING + "/" + name));
            }
        }
    }

    /**
     * 初始化 calendar
     *
     * @param calendar calendar
     * @return calendar
     */
    private static Calendar initCalender(Calendar calendar) {
        int month = calendar.get(Calendar.MONTH) + 1;
        //int month = LocalDate.now().getMonthValue() - 1; // 获取上传时间的月份
        if (month == 2) {
            int year = LocalDate.now().getYear(); // 获取年份
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { // 闰年
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+29);
            }else { // 平年
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+28);
            }
            return calendar;
        }
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+31);
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+30);
                break;
            default:
                break;
        }
        return calendar;
    }

    /**
     * 找出当前目录下的所有的文件夹
     *
     * @param dir 当前目录
     * @return 文件夹名称列表
     */
    private static List<String> findAllDirs(File dir) {
        List<String> dirNameList = new ArrayList<>();
        File[] files = dir.listFiles();
        if ( files != null && files.length > 0) {
            for (File temp : files) {
                if (temp.isDirectory()) {
                    dirNameList.add(temp.getName());
                }
            }
        }
        return dirNameList;
    }

    /**
     * 递归删除文件夹及文件夹下的内容
     *
     * @param file file
     */
    private static void deleteDirectoryLegacyIO(File file) {
        File[] files = file.listFiles();
        if (files != null && files.length > 0) {
            for (File temp : files) { // 递归删除子文件夹及子文件
                deleteDirectoryLegacyIO(temp); // 递归调用
            }
        }

        if (file.delete()) {
            System.out.println("删除文件：" + file.getName());
        }
    }
}
